// 
// SMS Notifier
// 
// By Aali Alikoski (aali@microsoft.com, http://blogs.msdn.com/aalialikoski)
// Originally developed as a sample for demonstrating Windows Mobile 5 managed APIs
// at Tampere University of Technology mobile development seminar
//

using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Xml.Serialization;

namespace SMSNotifier
{
    public class SMSsettings
    {
        private bool sendSMSToUnansweredCallers = true;
        public bool SendSMSToUnansweredCallers
        {
            get { return sendSMSToUnansweredCallers; }
            set { sendSMSToUnansweredCallers = value; }
        }

        private bool sendSMSOnlyWhenActiveAppointment = true;
        public bool SendSMSOnlyWhenActiveAppointment
        {
            get { return sendSMSOnlyWhenActiveAppointment; }
            set { sendSMSOnlyWhenActiveAppointment = value; }
        }

        private bool sendSMSOnlyWhenBusyAppointment = true;
        public bool SendSMSOnlyWhenBusyAppointment
        {
            get { return sendSMSOnlyWhenBusyAppointment; }
            set { sendSMSOnlyWhenBusyAppointment = value; }
        }

        private bool appendNextFreeSlotMessage = true;
        public bool AppendNextFreeSlotMessage
        {
            get { return appendNextFreeSlotMessage; }
            set { appendNextFreeSlotMessage = value; }
        }

        private bool addMissedCallInfoToCalendar = true;
        public bool AddMissedCallInfoToCalendar
        {
            get { return addMissedCallInfoToCalendar; }
            set { addMissedCallInfoToCalendar = value; }
        }

        private string smsMessageToSend = "I am currently busy, will call you back.";
        public string SMSMessageToSend
        {
            get { return smsMessageToSend; }
            set { smsMessageToSend = value; }
        }

        private string freeSlotTextFormat = "According to my calendar I'll be free around {0}.";
        public string FreeSlotTextFormat
        {
            get { return freeSlotTextFormat; }
            set { freeSlotTextFormat = value; }
        }

        [XmlIgnore]
        public string filename ="";

        public SMSsettings()
        {
        }

        public static SMSsettings ReadFromFile(string filename)
        {
            TextReader reader = null;
            SMSsettings s = new SMSsettings();
            s.filename = filename;
            try
            {
                reader = new StreamReader(filename);
                XmlSerializer serializer = new XmlSerializer(typeof(SMSsettings));
                s = (SMSsettings)serializer.Deserialize(reader);
                s.filename = filename;
            }
            catch
            {
            }
            finally
            {
                if (reader != null)
                    reader.Close();
            }
            return s;
        }

        public void Save()
        {
            try
            {
                TextWriter writer = new StreamWriter(filename, false); // do not append, overwrite instead
                XmlSerializer serializer = new XmlSerializer(typeof(SMSsettings));
                serializer.Serialize(writer, this);
                writer.Close();
            }
            catch (ArgumentException){
            }
        }
    }
}
